package com.wjc.http;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.util.Log;

import com.wjc.http.util.Equal;
import com.wjc.http.util.IO;

public class HttpResponseFactory
{
    /**
     * 
     * @param in
     * @param out
     * @return
     * @throws IOException
     */
    public static HttpResponse read(InputStream in, OutputStream out) throws IOException
    {
        HttpResponse httpResponse = new HttpResponse();
        StringBuffer headerBody = new StringBuffer();
        byte[] bytes = null;
        
        byte[] LF = new byte[]{0x0A};
        byte[] CRLF = new byte[]{0x0D, 0x0A};
       
        if((bytes = IO.readLine(in)) != null)
        { 
        	httpResponse.setFirstLine( new String(bytes));
        	headerBody.append(new String(bytes));
            if(!Equal.equals(bytes, CRLF) && !Equal.equals(bytes, LF))
            {
                String header = new String(bytes).trim();
                
                java.util.StringTokenizer st = new java.util.StringTokenizer(header, " ");
                Log.d("TGGGg",header);
                if(st.hasMoreTokens())
                {
                	
                    httpResponse.setHttpProtocol(st.nextToken());
                }
                
                if(st.hasMoreTokens())
                {
                    String value = st.nextToken();
                    
                    int statusCode = 200;
                    
                    try
                    {
                        statusCode = Integer.parseInt(value);
                    }
                    catch(NumberFormatException e){}
                   
                    httpResponse.setStatusCode(statusCode);
                }
                
                if(st.hasMoreTokens())
                {
                    httpResponse.setStatusString(st.nextToken());
                }
            }
        }
        
        while((bytes = IO.readLine(in)) != null)
        {
        	headerBody.append(new String(bytes));
            if(Equal.equals(bytes, CRLF) || Equal.equals(bytes, LF))
            {
                break;
            }
            
            String header = new String(bytes);
            
            int k = header.indexOf(":");
            
            if(k > -1)
            {
                String name  = header.substring(0, k).trim();
                String value = header.substring(k + 1).trim();
                
                httpResponse.setHeader(name, value);
            }
        }
        
        httpResponse.setHeaderBody( headerBody.toString() );
        
        httpResponse.setInputStream(in);
        httpResponse.setOutputStream(out);
        
        return httpResponse;
    }
}
 

